﻿using AbpCodeGenerater.Core;
using Scriban;
using Scriban.Runtime;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AbpCodeGenerater
{
    public partial class SimpleArchiveUIGenerateWindow : Form
    {
        protected AbpDomainClass AbpDomainClass { get; set; }

        protected TemplateModel TemplateModel { get; }

        protected TemplateContext TemplateContext { get; }

        protected StringBuilder RoutingText { get; }

        protected StringBuilder RouteText { get; }

        protected string TemplateDirectoryPath
        {
            get
            {
                return Path.Combine(Environment.CurrentDirectory, "Templates\\SimpleArchiveUI");
            }
        }

        public SimpleArchiveUIGenerateWindow(AbpDomainClass abpDomainClass)
        {
            InitializeComponent();
            AbpDomainClass = abpDomainClass;

            var scriptObject = new ScriptObject();
            scriptObject.Import(typeof(TemplateFuns));
            scriptObject.Import(typeof(TemplateModelExtension));
            TemplateModel = new TemplateModel(abpDomainClass);
            scriptObject.Import(TemplateModel);
            TemplateContext = new TemplateContext(scriptObject);

            txtRoot.Text = Path.Combine(($"{AbpDomainClass.AbpProject?.AbpSolution?.DirectioryPath ?? ""}").Replace("aspnet-core", ""), "angular", "projects", TemplateModel.ProjectGroupName.ToKebabCase());

            RoutingText = new StringBuilder();
            RoutingText.AppendLine("          {");
            RoutingText.AppendLine($"            path: '{TemplateModel.ClassName.ToKebabCase()}-list',");
            RoutingText.AppendLine($"            component: {TemplateModel.ClassName}ListComponent,");
            RoutingText.Append("          },");
            txtRoutingText.Text = RoutingText.ToString();

            RouteText = new StringBuilder();
            RouteText.AppendLine("      {");
            RouteText.AppendLine($"        path: '/basic-archives/item-masters/{TemplateModel.ClassName.ToKebabCase()}-list',");
            RouteText.AppendLine($"        name: 'BasicArchives::Menu:{TemplateModel.ProjectGroupName}.{TemplateModel.ParentMenu}.{TemplateModel.ClassName}',");
            RouteText.AppendLine($"        parentName: 'BasicArchives::Menu:{TemplateModel.ProjectGroupName}.{TemplateModel.ParentMenu}',");
            RouteText.AppendLine($"        requiredPolicy: 'Menus.{TemplateModel.ProjectGroupName}.{TemplateModel.ParentMenu}.{TemplateModel.ClassName}',");
            RouteText.AppendLine("        iconClass: 'inbox',");
            RouteText.Append("      },");
            txtRouteText.Text = RouteText.ToString();
        }

        private void btnSeletRoot_Click(object sender, EventArgs e)
        {
            var dialog = new FolderBrowserDialog();
            dialog.Description = "请选择一个目录";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                txtRoot.Text = dialog.SelectedPath;
            }
        }

        private void txtRoot_TextChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtRoot.Text))
            {
                btnGenerate.Enabled = false;
            }
            else
            {
                btnGenerate.Enabled = true;
            }

            if (!string.IsNullOrWhiteSpace(txtRoot.Text) && TemplateModel.ParentMenu != null)
            {
                txtListHtml.Text = Path.Combine(txtRoot.Text, "src", "lib", TemplateModel.ParentMenu.ToKebabCase(), $"{AbpDomainClass.Name.ToKebabCase()}-list", $"{AbpDomainClass.Name.ToKebabCase()}-list.component.html");
            }

            if (!string.IsNullOrWhiteSpace(txtRoot.Text) && TemplateModel.ParentMenu != null)
            {
                txtListLess.Text = Path.Combine(txtRoot.Text, "src", "lib", TemplateModel.ParentMenu.ToKebabCase(), $"{AbpDomainClass.Name.ToKebabCase()}-list", $"{AbpDomainClass.Name.ToKebabCase()}-list.component.less");
            }

            if (!string.IsNullOrWhiteSpace(txtRoot.Text) && TemplateModel.ParentMenu != null)
            {
                txtListTs.Text = Path.Combine(txtRoot.Text, "src", "lib", TemplateModel.ParentMenu.ToKebabCase(), $"{AbpDomainClass.Name.ToKebabCase()}-list", $"{AbpDomainClass.Name.ToKebabCase()}-list.component.ts");
            }

            if (!string.IsNullOrWhiteSpace(txtRoot.Text) && TemplateModel.ParentMenu != null)
            {
                txtDawEntityHtml.Text = Path.Combine(txtRoot.Text, "src", "lib", TemplateModel.ParentMenu.ToKebabCase(), $"{AbpDomainClass.Name.ToKebabCase()}-daw-entity", $"{AbpDomainClass.Name.ToKebabCase()}-daw-entity.component.html");
            }

            if (!string.IsNullOrWhiteSpace(txtRoot.Text) && TemplateModel.ParentMenu != null)
            {
                txtDawEntityLess.Text = Path.Combine(txtRoot.Text, "src", "lib", TemplateModel.ParentMenu.ToKebabCase(), $"{AbpDomainClass.Name.ToKebabCase()}-daw-entity", $"{AbpDomainClass.Name.ToKebabCase()}-daw-entity.component.less");
            }

            if (!string.IsNullOrWhiteSpace(txtRoot.Text) && TemplateModel.ParentMenu != null)
            {
                txtDawEntityTs.Text = Path.Combine(txtRoot.Text, "src", "lib", TemplateModel.ParentMenu.ToKebabCase(), $"{AbpDomainClass.Name.ToKebabCase()}-daw-entity", $"{AbpDomainClass.Name.ToKebabCase()}-daw-entity.component.ts");
            }
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            if (cboxListHtml.Checked)
            {
                var templateContent = File.ReadAllText(Path.Combine(TemplateDirectoryPath, "ListHtml.t"));
                var template = Template.Parse(templateContent);
                var fileContent = template.Render(TemplateContext);
                File.WriteAllText(txtListHtml.Text.MakeSurePathExists(), fileContent);
            }
            if (cboxListLess.Checked)
            {
                var templateContent = File.ReadAllText(Path.Combine(TemplateDirectoryPath, "ListLess.t"));
                var template = Template.Parse(templateContent);
                var fileContent = template.Render(TemplateContext);
                File.WriteAllText(txtListLess.Text.MakeSurePathExists(), fileContent);
            }
            if (cboxListTs.Checked)
            {
                var templateContent = File.ReadAllText(Path.Combine(TemplateDirectoryPath, "ListTs.t"));
                var template = Template.Parse(templateContent);
                var fileContent = template.Render(TemplateContext);
                File.WriteAllText(txtListTs.Text.MakeSurePathExists(), fileContent);
            }
            if (cboxDawEntityHtml.Checked)
            {
                var templateContent = File.ReadAllText(Path.Combine(TemplateDirectoryPath, "DawEntityHtml.t"));
                var template = Template.Parse(templateContent);
                var fileContent = template.Render(TemplateContext);
                File.WriteAllText(txtDawEntityHtml.Text.MakeSurePathExists(), fileContent);
            }
            if (cboxDawEntityLess.Checked)
            {
                var templateContent = File.ReadAllText(Path.Combine(TemplateDirectoryPath, "DawEntityLess.t"));
                var template = Template.Parse(templateContent);
                var fileContent = template.Render(TemplateContext);
                File.WriteAllText(txtDawEntityLess.Text.MakeSurePathExists(), fileContent);
            }
            if (cboxDawEntityTs.Checked)
            {
                var templateContent = File.ReadAllText(Path.Combine(TemplateDirectoryPath, "DawEntityTs.t"));
                var template = Template.Parse(templateContent);
                var fileContent = template.Render(TemplateContext);
                File.WriteAllText(txtDawEntityTs.Text.MakeSurePathExists(), fileContent);
            }

            MessageBox.Show("生成成功！");
        }

        private void btnClipRouting_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Clipboard.SetText(RoutingText.ToString());
        }

        private void btnClipRoute_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Clipboard.SetText(RouteText.ToString());
        }
    }
}
